package com.groupscore.score.service;

import com.groupscore.score.dao.PersonalScoreDao;
import com.groupscore.score.dao.ScoreDao;
import com.groupscore.score.pojo.PersonalScore;
import com.groupscore.score.pojo.Score;
import com.groupscore.score.pojogroup.GroupScore;
import com.groupscore.score.pojogroup.OptionScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ScoreService {

    @Autowired
    private ScoreDao scoreDao;
    @Autowired
    private PersonalScoreDao personalScoreDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 打分
     */
    public void add(List<Score> scoreList, String userId){

        for(Score s : scoreList){
            Score score = new Score();
            score.setId(idWorker.nextId()+"");
            score.setGroupId(s.getGroupId());
            score.setOptionId(s.getOptionId());
            score.setUserId(userId);
            score.setScore(s.getScore());
            score.setContent(s.getContent());
            scoreDao.save(score);
        }
    }

    /**
     * 打分
     */
    public void addPersonalScore(List<OptionScore> optionScores, String userId){

        for(OptionScore optionScore : optionScores){

            PersonalScore personalScore = new PersonalScore();
            personalScore.setId(idWorker.nextId()+"");
            personalScore.setPersonalName(optionScore.getName());
            personalScore.setOptionId(optionScore.getOptionId());
            personalScore.setScore(optionScore.getScore());
            personalScore.setUserId(userId);
            personalScore.setContent(optionScore.getContent());
            personalScore.setCreateTime(new Date());

            personalScoreDao.save(personalScore);
        }
    }

    /**
     * 修改分数
     */
    public void update(Score score){
        scoreDao.save(score);
    }

    /**
     * 删除分数
     */
    public void deleteById(String id){
        scoreDao.deleteById(id);
    }

    /**
     * 查询小组得分,及排名
     */
    public List<Map> groupScore(){
        return scoreDao.groupScore();
    }


    public List<Map> groupScoreByUserId(String userId){
        return scoreDao.groupScoreByUserId(userId);
    }

    public List<Map> allScore(){
        return scoreDao.allScore();
    }

    public List<Map> allPersonalScore(){
        return scoreDao.allPersonalScore();
    }

}
